<?php
namespace Admin\Controller;
use Think\Controller;
use Org\Util\Rbac;
/**
 * 后台首页控制器
 */
class AdminController extends Controller {

    /**
     * 后台控制器初始化
     */
    protected function _initialize(){
    	// 用户权限检查
    	if (C('USER_AUTH_ON') && !in_array(MODULE_NAME, explode(',', C('NOT_AUTH_MODULE')))) {
    	
    		if (!RBAC::AccessDecision()) {
    			//检查认证识别号
    			if (!$_SESSION [C('USER_AUTH_KEY')]) {
    				//跳转到认证网关
    				redirect(PHP_FILE . C('USER_AUTH_GATEWAY'));
    			}
    			// 没有权限 抛出错误
    			if (C('RBAC_ERROR_PAGE')) {
    				// 定义权限错误页面
    				redirect(C('RBAC_ERROR_PAGE'));
    			} else {
    				if (C('GUEST_AUTH_ON')) {
    					$this->assign('jumpUrl', PHP_FILE . C('USER_AUTH_GATEWAY'));
    				}
    				// 提示错误信息
    				$this->error(L('_VALID_ACCESS_'));
    			}
    		}
    	}
    	$gid=I('get.gid','','intval');
    	if(empty($gid)){
    		$gid=M('admin_node')->where(array('name'=>ACTION_NAME))->getField('menu_id');
//     		echo M('admin_node')->getLastSql();
//     		dump($gid);
    	}
       $this->assign('gid',$gid);
       $this->assign('__TOP_MENU__',$this->getTopMenu());
       $this->assign('__LEFT_MENU__',$this->getLeftMenu($gid));
    }

	protected function getTopMenu(){
		return M('admin_menu')->where(array('status'=>1))->select();
	}
	
	protected  function getLeftMenu($mid){
		$all=M('admin_node')->where(array('menu_id'=>$mid,'status'=>1,'is_menu'=>1))->order('sort desc')->select();
		$data=array();
 		//dump($all);
		foreach($all as $v){
			if($v['level']==2){
                $data[$v['id']]=!empty($data[$v['id']])?array_merge($v,$data[$v['id']]):$v;
				//$data[$v['id']]=$v;				
			}else if($v['level']==3){
				$data[$v['pid']]['child'][$v['id']]=$v;
			}
		}
 		//dump($data);
		return $data;
	}
}
